package com.zb.uniman.interceptor;

import com.zb.uniman.model.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Author:B
 * Description:
 */
public class TokenInterceptor implements HandlerInterceptor {

    @Autowired
    private Token token;

    /**
     * 测试正确
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        System.out.println("Pre Handle method is Calling-----------------");
        /** 读取配置 -----完成校验 */

//        String tokenName = token.getTokenName();
//        if (!tokenName.equals("dsa1231")) {
//            return false;
//        }
//        return true;

        /** 使用jwt完成校验 -----token实现会话*/
//        String token = request.getHeader("token");
//        if (token.isEmpty()) {
//            return false;
//        }
//        Claims claims = JwtUtil.parseJWT(token);
//        String subject = claims.getSubject();
//
//        if (!StringUtils.equals(subject, "customer")) {
//            return false;
//        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
}